
////////////////////////////PO Dialog//////////////////////////////////////
var calleeFieldForPODialog = null;
var callbackForPODialog = null;
function showPODialog(inputObj,po_cache,callback){
	render_jqPOTable(po_cache)
	calleeFieldForPODialog = inputObj;
	callbackForPODialog = callback;
	
	var dialog = jQuery('#ctx_common_search_po_tbl_div').dialog({
		autoOpen: false,
		title: 'Search PO',
		 height: 400,
		 width:400
	});
	
	dialog.dialog('open');
	return false;
}

function this_selectPO(){
	jQuery('#ctx_common_search_po_tbl_div').dialog('close');
	var selectedTRs = getSelectedRows("ctx_common_search_po_tbl");
	var selectedRecord = jQuery('#ctx_common_search_po_tbl').dataTable().fnGetData(selectedTRs[0]);
	dwr.util.setValue(calleeFieldForPODialog,selectedRecord.orderNumber+" ,Version"+selectedRecord.versionNumber);
	calleeFieldForPODialog.disabled = true;
	if(callbackForPODialog != null){
		eval("callbackForPODialog(selectedRecord)");
	}
	
	
}

function render_jqPOTable(po_cache) {
	ctx_supplier_searchPO = jQuery('#ctx_common_search_po_tbl').dataTable({
	
		"aaData" : po_cache,
		"bProcessing" : true,
		"bJQueryUI" : true,
		"bSortClasses" : false,
		"bFilter": false,
	   "bRetrieve":true,
		
		"aoColumns" :  [
			{
				"mDataProp" : null,
				"sClass": "alignCenter",
				"bSortable": false,
				"fnRender" : function(obj) {
					return "<input type='radio' >";
				},
				"bUseRendered" : false
			},   {
			"mDataProp" : "orderNumber",
			"sClass": "alignCenter",
			"fnRender" : function(obj) {
				var onum = obj.aData.orderNumber;
				return   onum ;
			},
			"bUseRendered" : false
		},  {
			"mDataProp" : "clientNameText",
			"sClass": "alignCenter"
			
		}, {
			"mDataProp" : "orderAmount",
			"sClass": "alignCenter"
		} ]
	});
	
	jQuery('#ctx_common_search_po_tbl tbody tr').live('click', function (event) {        
	    var aData = jQuery('#ctx_common_search_po_tbl').dataTable().fnGetData(this); // get datarow
	    var isRowSelected = jQuery(jQuery(this).find("td:first")).children()[0].checked; 
	    
	    if (isRowSelected)  // null if we clicked on title row
	    {
	    	jQuery(this).addClass('row_selected');
	    } else{
	    	jQuery(this).removeClass('row_selected');
	    }
	});
}

////////////////////////////UOM//////////////////////////////////////

var uom_cache = {
	    data: [
	{unitId:"EA",unitMeasure:"each",unitDesc:"each"},
	{unitId:"PK",unitMeasure:"pack",unitDesc:"pack"},
	{unitId:"HUR",unitMeasure:"hour",unitDesc:"hour"},
	{unitId:"KGM",unitMeasure:"kilogram",unitDesc:"kilogram"},
	{unitId:"JOU",unitMeasure:"joule",unitDesc:"joule"},
	{unitId:"LBR",unitMeasure:"pound",unitDesc:"pound"},
	{unitId:"MAW",unitMeasure:"megawatt",unitDesc:"megawatt"},
	{unitId:"ONZ",unitMeasure:"ounce",unitDesc:"ounce"},
	{unitId:"BX",unitMeasure:"box",unitDesc:"box"},
	{unitId:"CS",unitMeasure:"case",unitDesc:"case"},
	{unitId:"CT",unitMeasure:"carton",unitDesc:"carton"},
	{unitId:"$",unitMeasure:"US Dollar",unitDesc:"US Dollar"},
	{unitId:"GLL",unitMeasure:"gallon (US)",unitDesc:"gallon (US)"}
	

	
	    ]
}
calleeFieldForUOMDialog = null;
callbackForUOMDialog = null;
function render_jqUOMTable(uom_data) {
	ctx_supplier_searchPO = jQuery('#ctx_common_uom_tbl').dataTable({
	
		"aaData" : uom_data,
		"bProcessing" : true,
		"bJQueryUI" : true,
		"bSortClasses" : false,
		"bFilter": false,
		 "bRetrieve":true,
		"aoColumns" :  [
			{
				"mDataProp" : null,
				"sClass": "alignCenter",
				"bSortable": false,
				"fnRender" : function(obj) {
					return "<input type='radio' name='uom_radio' >";
				},
				"bUseRendered" : false
			},   {
			"mDataProp" : "unitId",
			"sClass": "alignCenter"
			
		},  {
			"mDataProp" : "unitMeasure",
			"sClass": "alignCenter"
			
		}, {
			"mDataProp" : "unitDesc",
			"sClass": "alignCenter"
		} ]
	});
	
	jQuery('#ctx_common_uom_tbl tbody tr').live('click', function (event) {        
	    var aData = jQuery('#ctx_common_uom_tbl').dataTable().fnGetData(this); // get datarow
	    var isRowSelected = jQuery(jQuery(this).find("td:first")).children()[0].checked; 
	    jQuery("#ctx_common_uom_tbl tbody tr").removeClass("row_selected");
	    if (isRowSelected)  // null if we clicked on title row
	    {
	    	jQuery(this).addClass('row_selected');
	    } 
	    
	    var selectedTRs = getSelectedRows("ctx_common_uom_tbl");
		var selectedRecord = jQuery('#ctx_common_uom_tbl').dataTable().fnGetData(selectedTRs[0]);
		dwr.util.setValue(calleeFieldForUOMDialog,selectedRecord.unitDesc);
		calleeFieldForUOMDialog = null;
		if(callbackForUOMDialog != null){
			eval("callbackForUOMDialog(selectedRecord)");
		}
		jQuery('#ctx_common_uom_tbl_div').dialog('close');
	});
}

function showUOMDialog(inputObj,uom_data,callback){
	render_jqUOMTable(uom_data)
	calleeFieldForUOMDialog = inputObj;
	callbackForUOMDialog = callback;
	
	var dialog = jQuery('#ctx_common_uom_tbl_div').dialog({
		autoOpen: false,
		title: 'Select A Unit',
		 height: 400,
		 width:400
	});
	
	dialog.dialog('open');
	return false;
}

///////////////////////////////Search Company///////////////////////////

//showSearchWindow
calleeFieldForCompanySerachDialog = null;
callbackForCompanySerachDialog = null;

function showCompanySearchDialog(inputObj,renderData,callback){
	render_jqSearchSupplier(renderData);
	calleeFieldForCompanySerachDialog = inputObj;
	callbackForCompanySerachDialog = callback;
	var dialog = jQuery('#ctx_buyer_search_supplier_tbl_div').dialog({
		autoOpen: false,
		title: 'Search in Preferred Suppliers',
		 height: 400,
		 width:400
	});
	
	dialog.dialog('open');
	return false;
}

//comp_preferred_cache

function render_jqSearchSupplier(renderData) {
	

	
	ctx_buyer_searchSupplier = jQuery('#ctx_buyer_search_supplier_tbl').dataTable({
		"bProcessing" : true,
		"aaData" : renderData,
		"bJQueryUI" : true,
		"bSortClasses" : false,
		"bRetrieve" : true,
		
		
		"aoColumns" : [
		{
			"mDataProp" : null,
			"sClass": "alignCenter",
			"bSortable": false,
			"fnRender" : function(obj) {
				return "<input type='radio' id='compsupp"+ obj.aData.companyId +"' name='compsuppGrp'>";
			},
			"bUseRendered" : false
		},  {
			"mDataProp" : "companyName",
			"sClass": "alignCenter",
			"bSortable": false,
			"bUseRendered" : false
		}, {
			"mDataProp" : null,
			"sType" : "date",
			"sClass": "alignCenter",
			"bSortable": false,
			"fnRender" : function(obj) {
				if(isEmptyString(obj.aData.companyDoingBusinessAs) && isEmptyString(obj.aData.companyDomain)){
					return 'Not Available';
				}else if(!isEmptyString(obj.aData.companyDoingBusinessAs)){
					return obj.aData.companyDoingBusinessAs;
				}else if(!isEmptyString(obj.aData.companyDomain)){
					return obj.aData.companyDomain;
				}
				return 'Not Available';
			},
			"bUseRendered" : false
		}		
		
		]
	});
	
	jQuery('#ctx_buyer_search_supplier_tbl tbody tr').live('click', function (event) {        
	    var aData = jQuery('#ctx_buyer_search_supplier_tbl').dataTable().fnGetData(this); // get datarow
	    var isRowSelected = jQuery(jQuery(this).find("td:first")).children()[0].checked; 
	    jQuery("#ctx_buyer_search_supplier_tbl tbody tr").removeClass("row_selected");
	    if (isRowSelected)  // null if we clicked on title row
	    {
	    	jQuery(this).addClass('row_selected');
	    } 
	    
	    var selectedTRs = getSelectedRows("ctx_buyer_search_supplier_tbl");
		var selectedRecord = jQuery('#ctx_buyer_search_supplier_tbl').dataTable().fnGetData(selectedTRs[0]);
		//dwr.util.setValue(calleeFieldForCompanySerachDialog,selectedRecord.unitDesc);
		calleeFieldForCompanySerachDialog = null;
		if(callbackForCompanySerachDialog != null){
			eval("callbackForCompanySerachDialog(selectedRecord)");
		}
		jQuery('#ctx_buyer_search_supplier_tbl_div').dialog('close');
	});
	
	

}




function this_resetSelectSupplier(){
	jQuery("input[name='compsuppGrp']").attr('checked',false);
}
